//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.eova.config;

import com.alibaba.fastjson.parser.ParserConfig;
import com.eova.aop.MetaObjectIntercept;
import com.eova.aop.UploadIntercept;
import com.eova.aop.UserSessionIntercept;
import com.eova.aop.eova.EovaIntercept;
import com.eova.common.plugin.quartz.QuartzPlugin;
import com.eova.common.utils.xx;
import com.eova.common.utils.web.RequestUtil;
import com.eova.copyright.CopyrightController;
import com.eova.copyright.EovaAuth;
import com.eova.core.IndexController;
import com.eova.core.admin.AdminController;
import com.eova.core.auth.AuthController;
import com.eova.core.button.ButtonController;
import com.eova.core.dict.DictController;
import com.eova.core.menu.MenuController;
import com.eova.core.meta.MetaController;
import com.eova.core.task.TaskController;
import com.eova.core.type.Convertor;
import com.eova.core.type.MysqlConvertor;
import com.eova.ext.beetl.BeetlEovaRenderFactory;
import com.eova.ext.jfinal.DbCaptchaCache;
import com.eova.handler.UrlBanHandler;
import com.eova.interceptor.AuthInterceptor;
import com.eova.interceptor.LoginInterceptor;
import com.eova.mod.EovaModConfig;
import com.eova.mod.EovaModPlugin;
import com.eova.model.Button;
import com.eova.model.Menu;
import com.eova.model.MetaField;
import com.eova.model.MetaObject;
import com.eova.model.Mod;
import com.eova.model.Role;
import com.eova.model.RoleBtn;
import com.eova.model.Session;
import com.eova.model.Task;
import com.eova.model.User;
import com.eova.model.Widget;
import com.eova.service.ServiceManager;
import com.eova.service.sm;
import com.eova.sql.dql.dialect.QueryDialect;
import com.eova.template.masterslave.MasterSlaveController;
import com.eova.template.office.OfficeController;
import com.eova.template.single.SingleController;
import com.eova.template.singlechart.SingleChartController;
import com.eova.template.singletree.SingleTreeController;
import com.eova.template.treetogrid.TreeToGridController;
import com.eova.user.UserController;
import com.eova.widget.WidgetController;
import com.eova.widget.form.FormController;
import com.eova.widget.grid.GridController;
import com.eova.widget.tree.TreeController;
import com.eova.widget.treegrid.TreeGridController;
import com.eova.widget.upload.UploadController;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.config.Routes.Route;
import com.jfinal.json.MixedJsonFactory;
import com.jfinal.kit.LogKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.plugin.druid.IDruidStatViewAuth;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.template.Engine;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.beetl.core.GroupTemplate;

public class EovaConfig extends JFinalConfig {
    public static boolean isDevMode = true;
    public static String APP_ID = "";
    public static String APP_SECRET = "";
    public static String EOVA_INDEX = "/";
    public static String EOVA_DBTYPE = "mysql";
    public static boolean isLowerCase = true;
    public static Convertor convertor = new MysqlConvertor();
    static HashMap<String, QueryDialect> queryDialectMap = new HashMap();
    protected static Map<String, String> props = new HashMap();
    protected static Map<String, String> exps = new HashMap();
    protected static Map<Integer, Set<String>> authUris = new HashMap();
    protected static Map<String, Set<String>> authFields = new HashMap();
    static HashMap<String, ActiveRecordPlugin> arps = new HashMap();
    private static EovaIntercept eovaIntercept = null;
    private static MetaObjectIntercept defaultMetaObjectIntercept = null;
    private static UserSessionIntercept userSessionIntercept = null;
    private static UploadIntercept uploadIntercept = null;
    private GroupTemplate gt = null;

    public EovaConfig() {
    }

    public void afterJFinalStart() {
        System.out.println(String.format("Starting Eova %s -> The most easy development platform", xx.getConfig("ver")));
        this.configEova();
    }

    public void beforeJFinalStop() {
    }

    public void configConstant(Constants me) {
        System.err.println("Config Constants Starting...");
        me.setEncoding("UTF-8");
        EovaInit.initConfig(props);
        isDevMode = xx.getConfigBool("devMode", true);
        me.setDevMode(isDevMode);
        if (isDevMode && "PRD".equals(xx.getConfig("env"))) {
            LogKit.warn("当前环境为生产环境, 并且开启了开发者模式, 如无必要请立即关闭, 避免对线上造成不可逆的后果!");
            LogKit.info("当前环境为生产环境, 并且开启了开发者模式, 如无必要请立即关闭, 避免对线上造成不可逆的后果!");
            LogKit.info("当前环境为生产环境, 并且开启了开发者模式, 如无必要请立即关闭, 避免对线上造成不可逆的后果!");
        }

        me.setMaxPostSize(524288000);
        me.setError500View("/eova/500.html");
        me.setError404View("/eova/404.html");
        me.setBaseUploadPath(xx.getConfig("static_root"));
        me.setJsonFactory(MixedJsonFactory.me());
        me.setJsonDatePattern("yyyy-MM-dd");
        ParserConfig.getGlobalInstance().setSafeMode(true);
        me.setCaptchaCache(new DbCaptchaCache());
        me.setConfigPluginOrder(1);
        boolean webappMode = xx.getConfigBool("webappMode", false);
        BeetlEovaRenderFactory rf = new BeetlEovaRenderFactory();
        rf.config(webappMode);
        me.setRenderFactory(rf);
        this.gt = rf.groupTemplate;
        this.exp();
        this.authUri();
        this.authField();
        this.license();
    }

    public void configRoute(Routes me) {
        System.err.println("Config Routes Starting...");
        me.setMappingSuperClass(true);
        me.add("/eova/admin", AdminController.class);
        me.add("/copyright", CopyrightController.class);
        me.add("/single_grid", SingleController.class);
        me.add("/single_tree", SingleTreeController.class);
        me.add("/single_chart", SingleChartController.class);
        me.add("/master_slave_grid", MasterSlaveController.class);
        me.add("/tree_grid", TreeToGridController.class);
        me.add("/office", OfficeController.class);
        me.add("/widget", WidgetController.class);
        me.add("/upload", UploadController.class);
        me.add("/form", FormController.class);
        me.add("/grid", GridController.class);
        me.add("/tree", TreeController.class);
        me.add("/treegrid", TreeGridController.class);
        me.add("/meta", MetaController.class);
        me.add("/menu", MenuController.class);
        me.add("/button", ButtonController.class);
        me.add("/auth", AuthController.class);
        me.add("/task", TaskController.class);
        me.add("/dict", DictController.class);
        me.add("/user", UserController.class);
        this.route(me);
        EOVA_INDEX = xx.getConfig("eova.index", "/");
        boolean flag = false;
        Iterator var3 = me.getRouteItemList().iterator();

        while(var3.hasNext()) {
            Route x = (Route)var3.next();
            if (x.getControllerKey().equals(EOVA_INDEX)) {
                flag = true;
            }
        }

        if (!flag) {
            me.add(EOVA_INDEX, IndexController.class);
        }

        try {
            var3 = EovaModPlugin.getModules().iterator();

            while(var3.hasNext()) {
                EovaModConfig mc = (EovaModConfig)var3.next();
                if (mc != null) {
                    me.add(EovaModPlugin.moduleRoutes(mc));
                }
            }
        } catch (Exception var5) {
            LogKit.error(String.format("load eova module routes error:%s", var5.getMessage()));
        }

    }

    public void configEngine(Engine me) {
        Map<String, Object> sharedVars = new HashMap();
        EovaConst.getPageConf().forEach((k, v) -> {
            sharedVars.put(k, props.get(v));
        });
        sharedVars.put("APP", EovaAuth.getEovaApp());
        EOVA_INDEX = EOVA_INDEX.endsWith("/") ? EOVA_INDEX : EOVA_INDEX + '/';
        LoginInterceptor.initNoLogin(EOVA_INDEX);
        sharedVars.put("INDEX", EOVA_INDEX);
        this.gt.setSharedVars(sharedVars);
        PageConst.init(sharedVars);
    }

    public void configPlugin(Plugins plugins) {
        System.err.println("Config Plugins Starting...");
        EovaDataSource.create(plugins);
        this.mappingEova((ActiveRecordPlugin)arps.get("eova"));
        this.mapping(arps);
        EovaDataSource.buildConvertor();
        this.plugin(plugins);
        ServiceManager.init();
        plugins.add(new EhCachePlugin());
        boolean isQuartz = xx.getConfigBool("isQuartz", true);
        if (isQuartz) {
            QuartzPlugin quartz = new QuartzPlugin();
            plugins.add(quartz);
        }

        plugins.add(new EovaModPlugin());
    }

    public void configInterceptor(Interceptors me) {
        System.err.println("Config Interceptors Starting...");
        me.add(new LoginInterceptor());
        me.add(new AuthInterceptor());
    }

    public void configEova() {
    }

    public void configHandler(Handlers me) {
        System.err.println("Config Handlers Starting...");
        DruidStatViewHandler dvh = new DruidStatViewHandler("/druid", new IDruidStatViewAuth() {
            public boolean isPermitted(HttpServletRequest request) {
                String sid = RequestUtil.getCookieStr(request, "eovasid", (String)null);
                if (sid == null) {
                    return false;
                } else {
                    User user = sm.login.getLoginUser(sid);
                    return user == null ? false : user.isAdmin();
                }
            }
        });
        me.add(dvh);
        me.add(new UrlBanHandler(".*\\.(html|tag|sql)", false));
    }

    private void mappingEova(ActiveRecordPlugin arp) {
        arp.addMapping("eova_session", Session.class);
        arp.addMapping("eova_object", MetaObject.class);
        arp.addMapping("eova_field", MetaField.class);
        arp.addMapping("eova_button", Button.class);
        arp.addMapping("eova_menu", Menu.class);
        arp.addMapping("eova_user", User.class);
        arp.addMapping("eova_role", Role.class);
        arp.addMapping("eova_role_btn", RoleBtn.class);
        arp.addMapping("eova_task", Task.class);
        arp.addMapping("eova_widget", Widget.class);
        arp.addMapping("eova_mod", Mod.class);
    }

    protected void mapping(HashMap<String, ActiveRecordPlugin> arps) {
    }

    protected void route(Routes me) {
    }

    protected void plugin(Plugins plugins) {
    }

    protected void exp() {
        exps.put("selectEovaFieldByObjectCode", "select en Field,cn Name from eova_field where object_code = ?;ds=eova");
        exps.put("selectEovaUser", "select id ID,name 姓名, login_id 帐号 from eova_user;ds=eova");
        exps.put("selectEovaRole", "select id id,name cn from eova_role;ds=eova");
    }

    protected void authUri() {
        HashSet<String> uris = new HashSet();
        uris.add("/meta/object/**");
        uris.add("/meta/fields/**");
        uris.add("/meta/dict/**");
        uris.add("/widget/**");
        uris.add("/upload/**");
        uris.add("/grid/updateCell");
        authUris.put(0, uris);
    }

    protected void authField() {
        addAuthField("eova_role_code.lv->1,2");
    }

    protected static void addAuthField(String rule) {
        String[] ss = rule.split("->");
        String key = ss[0];
        String b = ss[1];
        Set<String> set = (Set)authFields.get(key);
        if (set == null) {
            set = new HashSet();
        }

        ((Set)set).addAll(Arrays.asList(b.split(",")));
        authFields.put(key, set);
    }

    protected static void addAuthUri(String rule) {
        String[] ss = rule.split("->");
        String uri = ss[0];
        String s1 = ss[1];
        String[] rids = s1.split(",");
        String[] var5 = rids;
        int var6 = rids.length;

        for(int var7 = 0; var7 < var6; ++var7) {
            String x = var5[var7];
            Integer rid = xx.toInt(x.trim());
            Set<String> set = (Set)authUris.get(rid);
            if (set == null) {
                set = new HashSet();
            }

            ((Set)set).addAll(Arrays.asList(uri.split(",")));
            authUris.put(rid, set);
        }

    }

    protected void license() {
        APP_ID = xx.getConfig("app_id").trim();
        APP_SECRET = xx.getConfig("app_secret").trim();
    }

    public static EovaIntercept getEovaIntercept() {
        return eovaIntercept;
    }

    public static void setEovaIntercept(EovaIntercept eovaIntercept) {
        EovaConfig.eovaIntercept = eovaIntercept;
    }

    public static MetaObjectIntercept getDefaultMetaObjectIntercept() {
        return defaultMetaObjectIntercept;
    }

    public static void setDefaultMetaObjectIntercept(MetaObjectIntercept defaultMetaObjectIntercept) {
        EovaConfig.defaultMetaObjectIntercept = defaultMetaObjectIntercept;
    }

    public static UserSessionIntercept getUserSessionIntercept() {
        return userSessionIntercept;
    }

    public static void setUserSessionIntercept(UserSessionIntercept userSessionIntercept) {
        EovaConfig.userSessionIntercept = userSessionIntercept;
    }

    public static UploadIntercept getUploadIntercept() {
        return uploadIntercept;
    }

    public static void setUploadIntercept(UploadIntercept uploadIntercept) {
        EovaConfig.uploadIntercept = uploadIntercept;
    }

    public static Map<String, String> getProps() {
        return props;
    }

    public static HashMap<String, ActiveRecordPlugin> getArps() {
        return arps;
    }

    public static void setArps(HashMap<String, ActiveRecordPlugin> arps) {
        EovaConfig.arps = arps;
    }

    public static Map<String, String> getExps() {
        return exps;
    }

    public static Map<Integer, Set<String>> getAuthUris() {
        return authUris;
    }

    public static Map<String, Set<String>> getAuthFields() {
        return authFields;
    }

    public static QueryDialect getQueryDialect(String ds) {
        return (QueryDialect)queryDialectMap.get(ds);
    }
}
